package com.proyecto.xpdroid.util;


import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.widget.EditText;
import android.widget.TextView;

import com.droid.users.util.GastoHolder;
import com.droid.users.util.XPDroidConstants;
import com.proyecto.xpdroid.R;

public class XmlObj extends Activity 
{

    public String getXML(String xmlstring)
        throws XmlPullParserException, IOException
    {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        
        ArrayList<GastoHolder> objetos = null;
        GastoHolder currentObjeto = null;

        //xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
        xpp.setInput(new StringReader (xmlstring));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name = null;
         if(eventType == XmlPullParser.START_DOCUMENT) {
        	 objetos = new ArrayList();
             System.out.println("Start document");
         } else if(eventType == XmlPullParser.END_DOCUMENT) {
             System.out.println("End document");
         } else if(eventType == XmlPullParser.START_TAG) {
             name = xpp.getName();
             
             if (name.equalsIgnoreCase("objetivos")){
            	 currentObjeto = new GastoHolder();
             } else if (currentObjeto != null){
                 if (name.equalsIgnoreCase("nombre")){
                	 currentObjeto.setNombre(xpp.nextText());
                 } else if (name.equalsIgnoreCase("fecha_inicio")){
                	 String[] parts = xpp.nextText().split(" ");
                	 String fecha = parts[0]; // Tomo la parte de la fecha
                	 String hora = parts[1]; // Tomo la parte de la hora
                	 //currentObjeto.setFfinicio(xpp.nextText());
                	 currentObjeto.setFfinicio(fecha);
                	 currentObjeto.setHinicio(hora);
                 } else if (name.equalsIgnoreCase("fecha_fin")){
                	 //currentObjeto.setFfin(xpp.nextText());
                	 String[] parts = xpp.nextText().split(" ");
                	 String fecha = parts[0]; // Tomo la parte de la fecha
                	 String hora = parts[1]; // Tomo la parte de la hora
                	 //currentObjeto.setFfinicio(xpp.nextText());
                	 currentObjeto.setFfin(fecha);
                	 currentObjeto.setHfin(hora);
                 }
             	 else if (name.equalsIgnoreCase("monto")){
            	 currentObjeto.setMonto(xpp.nextText());
             }
             }
             
             System.out.println("Start tag "+xpp.getName());
         } else if(eventType == XmlPullParser.END_TAG) {
             name = xpp.getName();
             if (name.equalsIgnoreCase("objetivos") && currentObjeto != null){
            	 objetos.add(currentObjeto);
             }         	 
             System.out.println("End tag "+xpp.getName());
         } else if(eventType == XmlPullParser.TEXT) {
             System.out.println("Text "+xpp.getText());
         }
         eventType = xpp.next();
        }
                
        return printXML(objetos);
        }
 

	private String printXML(ArrayList<GastoHolder> objetos) {
		// TODO Auto-generated method stub
		String content = "";
		Iterator<GastoHolder> it = objetos.iterator();
		while(it.hasNext())
		{
			GastoHolder currObj  = it.next();
			content = content + "NOMBRE :" +  currObj.getNombre() + XPDroidConstants.newLine;
			//content = content + "fecha_inicio :" +  currObj.getFfinicio() + XPDroidConstants.newLine;
			content = content + "FECHA INICIO :" +  currObj.getFfinicio() + XPDroidConstants.newLine;
			content = content + "HORA INICIO :" +  currObj.getHinicio() + XPDroidConstants.newLine;
			content = content + "FECHA FIN :" +  currObj.getFfin() + XPDroidConstants.newLine;
			content = content + "HORA FIN :" +  currObj.getHfin() + XPDroidConstants.newLine;
			content = content + "MONTO :" +  currObj.getMonto() + XPDroidConstants.newLine;
		}

	return content;
	}
} 
